package com.example.localareanetworkchat

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import com.example.localareanetworkchat.fragment.MainViewModel
import com.example.localareanetworkchat.persistence.entity.User
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    val TAG = MainActivity::class.simpleName
    private var viewModel: MainViewModel ?= null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val navController = (supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment).navController
        navController.setGraph(R.navigation.nav_grap_chat_main)
        NavigationUI.setupWithNavController(bnv_chat_main, navController)

        viewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory(application)).get(MainViewModel::class.java)

        val user = intent.extras?.getParcelable<User>("user")
        val port = intent.extras?.getString("port")!!
        if (user != null) {
            viewModel?.user = user
            if (port.isNotEmpty()){
                viewModel?.port = port.toInt()
            }

            viewModel?.initChatHttpServer()
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        return super.onSupportNavigateUp()
    }

}